/*

This file sets up tables


*/

use "/projects/data/dataSTATA/combined/combined_productivity_energy_4digit_4m.dta", clear

* drop obs that would be excluded from regressions --> generate constant sample
ivreghdfe lelec_int  (lelec_price lelec_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
keep if e(sample)

ivreghdfe lco2_int_i_both  (lelec_price lelec_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
keep if e(sample)

ivreghdfe lbtu_int_i_both  (lelec_price lelec_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
keep if e(sample)

reghdfe lelec_price_init instr_B_cl instr_B_ng instr_B_pa instr_B_cl_init instr_B_ng_init instr_B_pa_init   [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
keep if e(sample)

gen age = year - firstyear

*>--------- define rounding function for regs---------------------<
* copied from rounding_regs.do Census file provided

capture program drop round
program define round, eclass
   mat A = e(b)
   forval j = 1/`=colsof(matrix(A))' { 
      mat A[1,`j'] = round(A[1,`j'],10^(floor(log10(abs(A[1,`j'])))-3))
   } 
   ereturn repost b = A

   if e(N)>=15&e(N)<=99 ereturn scalar N = round(e(N), 10)
   if e(N)>=100&e(N)<=999 ereturn scalar N = round(e(N), 50)
   if e(N)>=1000&e(N)<=9999 ereturn scalar N = round(e(N), 100)
   if e(N)>=10000&e(N)<=99999 ereturn scalar N = round(e(N), 500)
   if e(N)>=100000&e(N)<=999999 ereturn scalar N = round(e(N), 1000)
   if e(N)>=1000000 ereturn scalar N = round(e(N),10^(floor(log10(e(N)))-3))

   if e(N)<15 {
   scalar N_less15=1
   ereturn scalar N=.
   }

   if e(df_r)>=15&e(df_r)<=99 ereturn scalar df_r = round(e(df_r), 10)
   if e(df_r)>=100&e(df_r)<=999 ereturn scalar df_r = round(e(df_r), 50)
   if e(df_r)>=1000&e(df_r)<=9999 ereturn scalar df_r = round(e(df_r), 100)
   if e(df_r)>=10000&e(df_r)<=99999 ereturn scalar df_r = round(e(df_r), 500)
   if e(df_r)>=100000&e(df_r)<=999999 ereturn scalar df_r = round(e(df_r), 1000)
   if e(df_r)>=1000000 ereturn scalar df_r = round(e(df_r),10^(floor(log10(e(df_r)))-3))

   if e(df_r)<15 {
   scalar df_less15=1
   ereturn scalar df_r=.
   }


   capture ereturn scalar r2 = round(e(r2),10^(floor(log10(e(r2)))-3))
   capture ereturn scalar r2_a = round(e(r2_a),10^(floor(log10(e(r2_a)))-3))
   capture ereturn scalar F = round(e(F),10^(floor(log10(e(F)))-3))
   capture ereturn scalar rmse = round(e(rmse),10^(floor(log10(e(rmse)))-3))
   capture ereturn scalar mss = round(e(mss),10^(floor(log10(e(mss)))-3))
   capture ereturn scalar rss = round(e(rss),10^(floor(log10(e(rss)))-3))
   capture ereturn scalar rkf = round(e(rkf),10^(floor(log10(e(rkf)))-3))

end


*>-------- Table A.12

* define increasing using past lag

preserve
	use "/projects/data/dataSTATA/economic/seds_elec_prices.dta", clear
	encode postalst, gen(st_no)
	xtset st_no year
	gen Lavg_state_price = L.seds_price
	gen increasing = 0
	replace increasing = 1 if seds_price > Lavg_state_price
	keep postalst year increasing
	tempfile increasing
	sa `increasing', replace	
restore

preserve
merge m:1 postalst year using `increasing', assert(2 3) keep(3) nogen

foreach var of varlist instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init lelec_price lelec_price_init {
	gen `var'Xinc = `var' * increasing	
}



* intensity 
ivreghdfe lelec_int (lelec_price  lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init   instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe lelec_int (lelec_price  lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init  instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

ivreghdfe lco2_int_i_both (lelec_price lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init  instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe lbtu_int_i_both (lelec_price  lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init  instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

esttab using "/projects/results/tables/dynamics_intensity.xls", b(3) se(3) replace star(* 0.10 ** 0.05 *** 0.01) keep(lelec_price lelec_price_init lelec_price_initXinc) mgroups("Electricity Intensity" "Electricity Intensity, Elec. Intensive Industries" "CO2 Intensity" "BTU Intensity" , pattern(1 1 1 1)) coeflabels(lelec_price log(Current_Electricity_Price) lelec_price_init log(Initial_Electricity_Price) lelec_price_initXinc log(Initial_Electricity_Price)XIncreasing) scalars("rkf K-P F stat") sfmt(1)
eststo clear 



* productivity 
ivreghdfe we_scale  (lelec_price  lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init   instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe we_scale  (lelec_price  lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init  instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

ivreghdfe wh  (lelec_price lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init  instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe wh (lelec_price  lelec_price_init lelec_price_initXinc = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init  instr_B_cl_initXinc instr_B_ng_initXinc instr_B_pa_initXinc) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round 


esttab using "/projects/results/tables/dynamics_productivity.xls", b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) keep(lelec_price lelec_price_init lelec_price_initXinc) mgroups("Energy Productivity" "Energy Productivity, Elec. Intensive Industries" "Total Factor Productivity" "Total Factor Productivity, Elec. Intensive Industries" , pattern(1 1 1 1)) coeflabels(lelec_price log(Current_Electricity_Price) lelec_price_init log(Initial_Electricity_Price) lelec_price_initXinc log(Initial_Electricity_Price)XIncreasing) scalars("rkf K-P F stat") sfmt(1)
eststo clear
	
restore

*>------- Table A.12 ----------------------------------------------------------------<
* looks similar to main sample 

preserve
	use "/projects/data/dataSTATA/economic/init_prices_avg.dta", clear
	replace firstyear = firstyear + 1
	ren elec_price_init_avg Lelec_price_init1
	ren lelec_price_init Llelec_price_init1
	tempfile lagPrices
	sa `lagPrices', replace
restore

preserve
	use "/projects/data/dataSTATA/economic/init_prices_avg2.dta", clear
	replace firstyear = firstyear + 1
	ren elec_price_init_avg2 Lelec_price_init2
	ren lelec_price_init_avg2 Llelec_price_init2
	tempfile lagPrices2
	sa `lagPrices2', replace
restore

preserve
	use "/projects/data/dataSTATA/economic/seds_elec_prices.dta", clear
	encode postalst, gen(st_no)
	xtset st_no year
	gen Lavg_state_price = L.seds_price
	ren year firstyear
	replace firstyear = firstyear + 1
	gen Llelec_price_init3 = log(Lavg_state_price)
	keep postalst Llelec_price_init3 firstyear
	tempfile lagPrices3
	sa `lagPrices3', replace
restore

merge m:1 firstyear state bestnaics using `lagPrices', assert(1 2 3) keep(1 3) nogen
merge m:1 firstyear state using `lagPrices2', assert(1 2 3) keep(1 3) nogen
merge m:1 firstyear postalst using `lagPrices3', assert(1 2 3) keep(1 3) nogen

gen Llelec_price_init = .
replace Llelec_price_init = Llelec_price_init1 if mi(Llelec_price_init)
replace Llelec_price_init = Llelec_price_init2 if mi(Llelec_price_init)
replace Llelec_price_init = Llelec_price_init3 if mi(Llelec_price_init)

* lag the instrument
preserve
keep firstyear state instr_B_cl_init instr_B_ng_init instr_B_pa_init
duplicates drop firstyear state, force
replace firstyear = firstyear + 1
ren instr_B_cl_init Linstr_B_cl_init
ren instr_B_ng_init Linstr_B_ng_init
ren instr_B_pa_init Linstr_B_pa_init
tempfile LagIV
sa `LagIV', replace
restore

* build IV for 1976
preserve
	use "/projects/data/dataSTATA/temp/ivvars_init.dta", clear
	foreach var in B V {
		foreach var2 in cl ng pa {
			gen L0elecShare`var'_`var2'_1976 = L0elecShare`var'_`var2' if firstyear == 1976
			bys postalst: egen elecShare`var'_`var2'_1976 = mean(L0elecShare`var'_`var2'_1976)
		}
	}
	replace firstyear = firstyear + 1
	foreach var2 in cl ng pa {		
		gen Linstr_B_`var2'_init_1976 = efPriceNational`var2'_init*elecShareB_`var2'_1976
	} 
	keep firstyear postalst Linstr_B*
	tempfile 1975IV
	sa `1975IV', replace
restore

merge m:1 firstyear state using `LagIV', assert(1 2 3) keep(1 3) nogen
merge m:1 firstyear postalst using `1975IV', assert(1 2 3) keep(1 3) nogen
foreach var2 in cl ng pa{		
	replace Linstr_B_`var2'_init = Linstr_B_`var2'_init_1976 if mi(Linstr_B_`var2'_init)
} 
drop Linstr_B_cl_init_1976 Linstr_B_ng_init_1976 Linstr_B_pa_init_1976

* intensity outcomes
ivreghdfe lelec_int  (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe lelec_int  (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

ivreghdfe lco2_int_i_both  (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe lbtu_int_i_both  (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

esttab using "/projects/results/tables/intensity_lags.xls", b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) keep(lelec_price Llelec_price_init) mtitles("Electricity Intensity" "Electricity Intensity, Elec. Intensive Industries" "CO2 Intensity" "BTU Intensity") coeflabels(lelec_price log(Current_Electricity_Price) Llelec_price_init log(Lag_Initial_Electricity_Price)) scalars("rkf K-P F stat") sfmt(1)
eststo clear 


* productivity outcomes
ivreghdfe we_scale  (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe we_scale  (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

ivreghdfe wh  (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe wh (lelec_price Llelec_price_init = instr_B_cl instr_B_ng instr_B_pa  Linstr_B_cl_init Linstr_B_ng_init Linstr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

esttab using "/projects/results/tables/productivity_lags.xls", b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) keep(lelec_price Llelec_price_init) mtitles("Energy Productivity" "Energy Productivity, Elec. Intensive Industries" "Total Factor Productivity" "Total Factor Productivity, Elec. Intensive Industries") coeflabels(lelec_price log(Current_Electricity_Price) Llelec_price_init log(Lag_Initial_Electricity_Price)) scalars("rkf K-P F stat") sfmt(1)
eststo clear 



*>------------------- Table A.8 and A.9 -----------------------------<

* both current and initial prices from SEDS

* intensity 
ivreghdfe lelec_int  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe lelec_int  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

ivreghdfe lco2_int_i_both  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe lbtu_int_i_both  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round


esttab using "/projects/results/tables/intensity_sedsprices.xls", b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) keep(lseds_price lseds_price_init) mtitles("Electricity Intensity" "Electricity Intensity, Elec. Intensive Industries" "CO2 Intensity" "BTU Intensity") coeflabels(lseds_price log(Current_EIA_Price) lseds_price_init log(Initial_EIA_Price)) scalars("rkf K-P F stat") sfmt(1)
eststo clear
						

				
* productivity

ivreghdfe we_scale  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe we_scale  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

ivreghdfe wh  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe wh  (lseds_price lseds_price_init = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

esttab using "/projects/results/tables/productivity_sedsprices.xls", b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) keep(lseds_price lseds_price_init) mtitles("Energy Productivity" "Energy Productivity, Elec. Intensive Industries" "Total Factor Productivity" "Total Factor Productivity, Elec. Intensive Industries") coeflabels(lseds_price log(Current_EIA_Price) lseds_price_init log(Initial_EIA_Price)) scalars("rkf K-P F stat") sfmt(1)
eststo clear 


*>------------ Table A.2 -----<

* full sample and electricity intensive industries -- energy outcomes
preserve

gen ind_group = 1
replace ind_group = 1 if inlist(naicscode3,331, 332)
replace ind_group = 2 if inlist(naicscode3,324, 327)
replace ind_group = 3 if inlist(naicscode3,325, 326)
replace ind_group = 4 if inlist(naicscode3, 333)
replace ind_group = 5 if inlist(naicscode3,334,335)
replace ind_group = 6 if inlist(naicscode3,336)
replace ind_group = 7 if inlist(naicscode3,311, 312)
replace ind_group = 8 if inlist(naicscode3,313, 314, 315, 316)
replace ind_group = 9 if inlist(naicscode3,321, 337)
replace ind_group = 10 if inlist(naicscode3,322, 323)
replace ind_group = 11 if inlist(naicscode3,339)


mat M = J(22, 4,.)

* round numerators and denominators for variables where that are ratios
do /projects/programs/codeSTATA/B_analysis/rounding_4sigdig_v2 pe tvs co2_int_i_both btu_int_i_both
gen  elec_int_round = pe / tvs

local i = 0
foreach y of numlist 1 2 3 4 5 6 7 8 9 10 11 {
	

	sum elec_int_round if ind_group == `y'
	gen mean_e = `r(mean)'
	gen se_e   = `r(sd)'
	gen N2 = `r(N)'
	sum co2_int_i_both if ind_group == `y'
	gen mean_co2 = `r(mean)'
	gen se_co2   = `r(sd)'
	sum btu_int_i_both if ind_group == `y'
	gen mean_btu = `r(mean)'
	gen se_btu   = `r(sd)'
	do /projects/programs/codeSTATA/B_analysis/rounding_4sigdig_v2 mean_e se_e mean_co2 se_co2 mean_btu se_btu
	do /projects/programs/codeSTATA/B_analysis/rounding_N.do N2
	
	local i = `i' + 1 
	sum mean_e
	mat M[`i',1] = `r(mean)'
	sum mean_co2
	mat M[`i',2] = `r(mean)'
	sum mean_btu
	mat M[`i',3] = `r(mean)'
		
	local i = `i' + 1 
	sum se_e
	mat M[`i',1] = (`r(mean)')
	sum se_co2
	mat M[`i',2] = (`r(mean)')
	sum se_btu
	mat M[`i',3] = (`r(mean)')
	sum N2
	mat M[`i',4] = `r(mean)'
	drop mean_e se_e mean_co2 se_co2 mean_btu se_btu N2

}

set linesize 255
svmat M
format M* %25.2f
keep M*
matrix colnames M = Electricity_Intensity CO2_Intensity BTU_Intensity N
matrix rownames M = IndustryGroup1 se IndustryGroup2 se IndustryGroup3 se IndustryGroup4 se IndustryGroup5 se IndustryGroup6 se IndustryGroup7 se IndustryGroup8 se IndustryGroup9 se IndustryGroup10 se IndustryGroup11 se 
esttab matrix(M) using "/projects/results/tables/summary_stats_ind.xls", replace
	
restore


*>----------------------- Table 6 --------------------------<

* age
ivreghdfe we_scale  (lelec_price c.age#c.lelec_price_init lelec_price_init= instr_B_cl instr_B_ng instr_B_pa  c.age#c.instr_B_cl_init instr_B_cl_init c.age#c.instr_B_ng_init  instr_B_ng_init c.age#c.instr_B_pa_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe we_scale  (lelec_price c.age#c.lelec_price_init lelec_price_init= instr_B_cl instr_B_ng instr_B_pa  c.age#c.instr_B_cl_init instr_B_cl_init c.age#c.instr_B_ng_init  instr_B_ng_init c.age#c.instr_B_pa_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

ivreghdfe wh  (lelec_price c.age#c.lelec_price_init lelec_price_init= instr_B_cl instr_B_ng instr_B_pa  c.age#c.instr_B_cl_init instr_B_cl_init c.age#c.instr_B_ng_init  instr_B_ng_init c.age#c.instr_B_pa_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo: round

ivreghdfe wh (lelec_price c.age#c.lelec_price_init lelec_price_init= instr_B_cl instr_B_ng instr_B_pa  c.age#c.instr_B_cl_init instr_B_cl_init c.age#c.instr_B_ng_init  instr_B_ng_init c.age#c.instr_B_pa_init instr_B_pa_init) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst), if elec_perc_ind > 0.7
eststo: round

esttab using "/projects/results/tables/heterogeneity_age_productivity.xls", b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) keep(lelec_price lelec_price_init c.age#c.lelec_price_init) mgroups("Energy Productivity" "Energy Productivity, Elec. Intensive Industries" "Total Factor Productivity" "Total Factor Productivity, Elec. Intensive Industries" , pattern(1 1 1 1 )) coeflabels(lelec_price log(Current_Electricity_Price) lelec_price_init log(Initial_Electricity_Price) c.age#c.lelec_price_init log(Initial_Electricity_Price)XAge) scalars("rkf K-P F stat") sfmt(1)
eststo clear

*>------------------------- Table A.11 --------<

gen ind_group = 1
replace ind_group = 1 if inlist(naicscode3,331, 332)
replace ind_group = 2 if inlist(naicscode3,324, 327)
replace ind_group = 3 if inlist(naicscode3,325, 326)
replace ind_group = 4 if inlist(naicscode3, 333)
replace ind_group = 5 if inlist(naicscode3,334,335)
replace ind_group = 6 if inlist(naicscode3,336)
replace ind_group = 7 if inlist(naicscode3,311, 312)
replace ind_group = 8 if inlist(naicscode3,313, 314, 315, 316)
replace ind_group = 9 if inlist(naicscode3,321, 337)
replace ind_group = 10 if inlist(naicscode3,322, 323)
replace ind_group = 11 if inlist(naicscode3,339)

foreach y of numlist 1 2 3 4 5 6 7 8 9 10 11 {
preserve
	gen ind = 0
	replace ind = 1 if ind_group == `y'
	gen lelec_price_initXind = lelec_price_init * ind
	gen instr_B_cl_initXind  = instr_B_cl_init * ind
	gen instr_B_ng_initXind  = instr_B_ng_init * ind
	gen instr_B_pa_initXind  = instr_B_pa_init * ind
	
	
	
	ivreghdfe lelec_int  (lelec_price lelec_price_init lelec_price_initXind = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init instr_B_cl_initXind instr_B_ng_initXind instr_B_pa_initXind) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo:round

ivreghdfe lco2_int_i_both  (lelec_price lelec_price_init lelec_price_initXind = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init instr_B_cl_initXind instr_B_ng_initXind instr_B_pa_initXind) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo:round

ivreghdfe lbtu_int_i_both  (lelec_price lelec_price_init lelec_price_initXind = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init instr_B_cl_initXind instr_B_ng_initXind instr_B_pa_initXind) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo:round

ivreghdfe we_scale  (lelec_price lelec_price_init lelec_price_initXind = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init instr_B_cl_initXind instr_B_ng_initXind instr_B_pa_initXind) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo:round

ivreghdfe wh  (lelec_price lelec_price_init lelec_price_initXind = instr_B_cl instr_B_ng instr_B_pa  instr_B_cl_init instr_B_ng_init instr_B_pa_init instr_B_cl_initXind instr_B_ng_initXind instr_B_pa_initXind) [pw=wt_cmf], absorb(i.y##i.firstyear##i.bestnaics i.fipsst##i.bestnaics) cluster(fipsst)
eststo:round

	if `y' == 1 {
	esttab using "/projects/results/tables/ind_heterogeneity.xls", title(Industry Group `y') b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) keep(lelec_price lelec_price_init lelec_price_initXind) coeflabels(lelec_price log(Current_Electricity_Price)  lelec_price_init log(Initial_Electricity_Price) lelec_price_initXind log(Initial_Electricity_Price)XInd) scalars("rkf K-P F stat") sfmt(1)
	eststo clear 
		
	}
	else {
	esttab using "/projects/results/tables/ind_heterogeneity.xls", title(Industry Group `y') b(3) se(3) obslast append star(* 0.10 ** 0.05 *** 0.01) keep(lelec_price lelec_price_init lelec_price_initXind) coeflabels(lelec_price log(Current_Electricity_Price)  lelec_price_init log(Initial_Electricity_Price) lelec_price_initXind log(Initial_Electricity_Price)XInd) scalars("rkf K-P F stat") sfmt(1)
	eststo clear 
	}

restore
	
}
